package com.ssm.test;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

public class ListDemo {

    public static String listToStr(List<String> list) {
        StringBuffer str = new StringBuffer();
        Random random = new Random(10);
        Map<Integer, List<String>> group = list.parallelStream().collect(Collectors.groupingBy(e -> random.nextInt(100)));
        group.values().parallelStream().forEach(e -> e.forEach(str::append));
        System.out.println("method1: " + str.toString());
        return str.toString();
    }

    public static String list2Str(List<String> list, final int nThreads) throws Exception {
        if (list == null || list.isEmpty()) {
            return null;
        }

        StringBuffer ret = new StringBuffer();
        int size = list.size();
        ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
        List<Future<String>> futures = new ArrayList<Future<String>>(nThreads);

        for (int i = 0; i < nThreads; i++) {
//            final List<String> subList = list.subList(size / nThreads * i, size / nThreads * (i + 1));
            final List<String> subList;
            if (i == nThreads - 1) {
                subList = list.subList(i * nThreads, size);
            } else {
                subList = list.subList(size / nThreads * i, size / nThreads * (i + 1));
            }

            Callable<String> task = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    Map sb = new HashMap();
                    for (String str : subList) {
                        sb.put(str, str);
                    }
                    return sb.toString();
                }
            };
            futures.add(executorService.submit(task));
        }

        for (Future<String> future : futures) {
            ret.append(future.get());
        }
        executorService.shutdown();
        System.out.println("futureRet: " + ret.toString());
        return ret.toString();
    }


    public static void main(String[] args) throws InterruptedException, ExecutionException {

        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 1000000; i++) {
            list.add(i);
        }


        listSub(list);

    }

    private static void listSub(List<Integer> list) throws InterruptedException, ExecutionException {
        // 线程数
        int threadCount = 10;
        long sum = 0;
        // 创建一个线程池
        ExecutorService exec = Executors.newFixedThreadPool(threadCount);
        List<Callable<Long>> callList = new ArrayList<Callable<Long>>();

        int len = list.size() / threadCount;// 分割段数
        if (len == 0) {
            threadCount = list.size();
            len = list.size() / threadCount;

        }
        for (int i = 0; i < threadCount; i++) {
            final List<Integer> subList;
            if (i == threadCount - 1) {
                subList = list.subList(i * len, list.size());
            } else {
                subList = list.subList(i * len, len * (i + 1) > list.size() ? list.size() : len * (i + 1));
            }
            callList.add(new Callable<Long>() {
                public Long call() throws Exception {
                    long subSum = 0L;
                    for (Integer i : subList) {
                        subSum += i;
                    }
                    System.out.println("分配给线程=" + Thread.currentThread().getName() + "和为:" + subSum);
                    return subSum;
                }
            });
        }

        List<Future<Long>> futureList = exec.invokeAll(callList);
        for (Future<Long> future : futureList) {
            sum += future.get();
        }
        exec.shutdown();
        System.out.println("最后统计为:" + sum);
    }
}

